﻿using System;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using EbbinghausWord.BLL;
using EbbinghausWord.Common;
using EbbinghausWord.DAL;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

namespace EbbinghausWord.Pages
{
    public partial class LoadingPage : PhoneApplicationPage
    {
        public LoadingPage()
        {
            InitializeComponent();

            //var timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(33)};
            //timer.Tick += delegate { try { FrameworkDispatcher.Update(); } catch
            //{
            //    MessageBox.Show("error");
            //} };
            //timer.Start();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            var thread = new Thread(InitEnvironment);
            thread.Start(); 
            
            if (new UserConfig().IsAutoPlayVoice)
            {
                //是否播放音乐的检查
                if (MediaPlayer.Queue.Count > 0)
                {
                    if (MessageBox.Show("设备正在播放音乐，与单词自动发音功能冲突，您是否要停止播放音乐，是则停止音乐播放，否则禁用自动发音功能。", "后台播放", MessageBoxButton.OKCancel)
                        == MessageBoxResult.OK)
                    {
                        if (MediaPlayer.Queue.Count > 0)
                        {
                            FrameworkDispatcher.Update();
                            MediaPlayer.Pause();
                            FrameworkDispatcher.Update();
                        }
                    }
                    else
                    {
                        new UserConfig().IsAutoPlayVoice = false;
                    }
                }
            }
        }

        /// <summary>
        /// 应用环境初始化
        /// </summary>
        private void InitEnvironment()
        {
            var et = new ExecuteTimer(true);

            ShowMessageOnUI("正在加载词典");
            DictionaryManager.InitDb();
            et.Show("InitDB");

            //初始化当前单词本
            BookDAL.InitCurrentBookWord();

            //初始化用户单词本
            ShowMessageOnUI("正在加载用户文件");
            WordBookManager.InitUserWord();
            et.Show("InitUserWord");

            //保存成绩
            ShowMessageOnUI("正在加载成绩");
            ScoreManager.InitScore();
            et.Show("InitScore");

            

            Dispatcher.BeginInvoke(() =>
                NavigationService.Navigate(new UserConfig().UserBookId == 0
                                               ? new Uri(@"/Pages/SelectBookPage.xaml", UriKind.RelativeOrAbsolute)
                                               : new Uri(@"/Pages/ReviewWordPage.xaml", UriKind.RelativeOrAbsolute))
            );
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            //隐藏自己的踪迹
            NavigationService.RemoveBackEntry();
        }

        /// <summary>
        /// 在UI线程中，显示提示信息
        /// </summary>
        /// <param name="message"></param>
        private void ShowMessageOnUI(string message)
        {
            Dispatcher.BeginInvoke(() =>
                                       {
                                           PageTitle.Text = message;
                                       });
        }
    }
}